package com.chinasoft.news.service.impl;

import com.chinasoft.news.customexception.BussinessException;
import com.chinasoft.news.mapper.ArticlesMapper;
import com.chinasoft.news.pojo.*;
import com.chinasoft.news.service.ArticlesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class ArticlesServiceImpl implements ArticlesService {

    ArticlesMapper articlesMapper;


    @Override
    public Boolean addArticles(ArticlesAddVo addVo, String file, Integer userId) {
        List<Articles> articles1 = articlesMapper.selectByTitle(addVo.getTitle());

        if(articles1.size()==2){
            throw new BussinessException("title重复"+addVo.getTitle(), ExType.BussArticlesNameFail);
        }

        Articles articles = new Articles();
        articles.setAuthor_id(userId);
        articles.setContent(addVo.getContent());
        articles.setCover_img(file);
        articles.setIs_delete(false);
        articles.setCate_id(addVo.getCate_id());
        articles.setPub_date(new Date().toString());
        articles.setTitle(addVo.getTitle());
        articles.setState(addVo.getState());
        int i = articlesMapper.insertArticle(articles);
        if(i!=1){
            return false;
        }else {
            return true;
        }
    }


    @Override
    public List<ArticlesAndCate> getArticlesAll(Integer pagenum,Integer pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<ArticlesAndCate> articlesAndCates = articlesMapper.selectAll();
        return articlesAndCates;
    }

    @Override
    public PageInfo<ArticlesAndCate> getPageSelect(Integer cate_id,String state) {
        List<ArticlesAndCate> articles = articlesMapper.selectPageSelect(cate_id,state);
        PageInfo<ArticlesAndCate> pageInfo = new PageInfo<>(articles);
        return pageInfo;

    }

    @Override
    public List<ArticlesAndCate> getSelectByCateIdAndState(Integer pagenum, Integer pagesize, Integer cate_id, String state) {
        PageHelper.startPage(pagenum,pagesize);

        List<ArticlesAndCate> articlesAndCates = articlesMapper.selectBySelect(cate_id, state);

        return articlesAndCates;

    }

    @Override
    public PageInfo<ArticlesAndCate> getPageCateId(Integer cate_id) {
        List<ArticlesAndCate> articles = articlesMapper.selectByCateId(cate_id);
        PageInfo<ArticlesAndCate> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public List<ArticlesAndCate> getByCateId(Integer pagenum, Integer pagesize, Integer cate_id) {
        PageHelper.startPage(pagenum,pagesize);
        List<ArticlesAndCate> articlesAndCates = articlesMapper.selectByCateId(cate_id);
        return  articlesAndCates;
    }

    @Override
    public PageInfo<ArticlesAndCate> getPageState(String state) {
        List<ArticlesAndCate> articles = articlesMapper.selectPageState(state);
        PageInfo<ArticlesAndCate> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public List<ArticlesAndCate> getByState(Integer pagenum, Integer pagesize, String state) {
        PageHelper.startPage(pagenum,pagesize);
        List<ArticlesAndCate> articlesAndCates = articlesMapper.selectByState(state);
        return  articlesAndCates;
    }

    @Override
    public PageInfo<ArticlesAndCate> getPagesAll() {
        List<ArticlesAndCate> articles = articlesMapper.selectPage();
        PageInfo<ArticlesAndCate> pageInfo = new PageInfo<>(articles);
        return pageInfo;

    }



    @Override
    public Boolean deleteArticles(Integer id) {

        int i = articlesMapper.deleteArticles(id);
        if(i!=1){
            return  false;
        }else {
            return true;
        }
    }

    @Override
    public Articles getArticlesById(Integer id) {
        Articles articles = articlesMapper.selectById(id);
        return articles;
    }

    @Override
    public Boolean updateArticlesById(String title, String state, Integer id) {
        List<Articles> articles = articlesMapper.selectByTitle(title);

        if(articles.size()==2){
            throw new BussinessException("title重复"+title, ExType.BussArticlesNameFail);
        }
        int i = articlesMapper.updateById(title, state, id);
        if(i!=1){
            throw new BussinessException("文章更新失败"+id, ExType.BussArticlesFail);
        }
        return true;
    }


}
